<?php


use think\migration\Migrator;



class CreateTopicTbl extends Migrator
{
    const TBL_NAME = 'topic';
    const RELATED_TBL_NAME = 'category';
    const USER_TAB_NAME = 'user';



    public function change()
    {
        // create the table
        $table  =  $this->table(addTablePrefix(self::TBL_NAME),[
            'engine'=>'InnoDB',
            'comment' => '话题表',
            'signed' => false
        ]);
        $table
            ->addColumn('name', 'string',array('limit'  =>  17,'default'=>'','comment'=>'话题名称'))
            ->addColumn('desc', 'string',array('limit'  =>  255,'default'=>'','comment'=>'话题描述'))
            ->addColumn('image', 'string',array('limit'  =>  125,'default'=>'','comment'=>'话题封面'))
            ->addColumn('count', 'integer',array('signed' => false, 'default'=>0,'comment'=>'收录文章'))
            ->addColumn('status', 'integer',array('default'=>false,'comment'=>'是否完结 0连载 1完结'))
            ->addColumn('user_id', 'integer',array('signed' => false, 'comment'=>'创建者'))
            ->addColumn('category_id', 'integer',array('signed' => false, 'comment'=>'所属分类'))
            ->addColumn('create_time', 'integer',array('signed' => false, 'default'=>0,'comment'=>'创建时间'))
            ->addColumn('update_time', 'integer',array('signed' => false, 'default'=>0,'comment'=>'修改时间'))
            ->addIndex(array('name'))
            ->addIndex(array('name','user_id'), array('unique'  =>  true))
            ->addIndex(array('create_time'))
            ->addForeignKey('category_id', addTablePrefix(self::RELATED_TBL_NAME), 'id', ['delete'=> 'CASCADE', 'update'=> 'NO_ACTION'])
            ->addForeignKey('user_id', addTablePrefix(self::USER_TAB_NAME), 'id', ['delete'=> 'CASCADE', 'update'=> 'NO_ACTION'])
            ->create();
    }
}
